import datetime

from common.email_helper import email_send_with_appendix_list
from common.logger_helper import g_wlogger
from common.pandas_helper import PandasHelper
from k3_data_report.business.cgtj_db import Cgtj
from k3_data_report.business.chycbj_db import Chycbj
from k3_data_report.business.config import bussiness_cal_fields

__author__ = "Ypf"
_date_ = "2019/8/1-14:14"


def chycbj(time1):
    db_sql = bussiness_cal_fields['k3_caozuo']
    check_sql = db_sql["insert_check_sql"] % ('k3chycbj', time1)
    check_dic = Cgtj.check_cf(check_sql)
    get_1_sql = db_sql["chyc_1_sql"]
    dic_2=  Chycbj.get_dic(get_1_sql)
    get_3_sql = db_sql["chyc_3_sql"]
    dic_3 = Chycbj.get_dic(get_3_sql)
    insert_sql = db_sql["insert_chyc"]
    if len(check_dic) == 0:
        Chycbj.insert(insert_sql, dic_2, time1, 1)
        Chycbj.insert(insert_sql, dic_3, time1, 3)
    else:
        g_wlogger.winfo(f"chycbj-{time1}:target_full_df, all data is empty.")


def create_df_chycbj(time1=None):
            db_sql = bussiness_cal_fields['k3_caozuo']
            get_1_sql = db_sql["chyc_1_sql"]
            dic_1 = Chycbj.get_dic(get_1_sql)
            d = datetime.datetime.now()
            time_1 = d.replace(month=d.month - 1)
            time_2 = d.replace(month=d.month - 2)
            time_3 = d.replace(month=d.month - 3)
            df_data_list = []
            # [qdrq, ghdw, ywy, kf, bh, khhth, khbm, ggxh, pp, jhrq, sjhsdj, wchsl,kcsl  qdrq,ghdw,khjl,xszl,fwhth,khhth,khlh,ggxh,pp,htjq,sjhsdj,wjhsl,kcsl,]
            for key in dic_1:
                com_time = datetime.datetime.strptime(dic_1[key][9], "%Y-%m-%d")
                if time_2 < com_time < time_1:
                    dic_1[key].extend(
                        [float(dic_1[key][11]), float(dic_1[key][11]) * float(dic_1[key][10]), 0, 0, 0, 0])
                elif time_3 < com_time < time_2:
                    dic_1[key].extend(
                        [0, 0, float(dic_1[key][11]), float(dic_1[key][11]) * float(dic_1[key][10]), 0, 0])
                elif com_time < time_3:
                    dic_1[key].extend(
                        [0, 0, 0, 0, float(dic_1[key][11]), float(dic_1[key][11]) * float(dic_1[key][10])])
                index = dic_1[key][12]
                del dic_1[key][12]
                dic_1[key].append(index)
                end_tuple = tuple(dic_1[key])
                df_data_list.append(end_tuple)
            columns = ["签订日期", "购货单位", "客户经理", "销售助理", "飞五合同号", "客户合同号", "客户料号", "规格型号", "品牌", "合同交期", "实际含税单价",
                       "未交货数量", "已到交期一个月以上未出货数量", "已到交期一个月金额", "已到交期两个月以上未出货数量", "已到交期两个月金额", "已到交期三个月以上未出货数量",
                       "超期三个月以上金额", "是否在库（并填写库存数量)"]
            k3chyc_df = PandasHelper.create_dataframe(data=df_data_list, columns=columns)
            return  k3chyc_df
def chycbj_main(month):
    try:
        chycbj(month)
        return True
    except Exception as e:
        g_wlogger.werror(f"chycbj_main has error:{e} with:{month}")
        return False
if __name__ == "__main__":
    time1 = "2019-09"
    chycbj(time1)


